package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.pi.common.CodesConstants;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.fac.service.InforCommonWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.TwaDocAsnHeader;
import com.pi.wms.service.AsnService;
import com.pi.wms.service.KitAsnService;
import com.pi.wms.service.SmAsnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/inbound/smasn")
public class SmAsnController extends BaseAsnController<TwaDocAsnHeader, Long> {

    @Autowired
    private SmAsnService service;

    @Autowired
    private InforCommonWorkService commonStorageService;

    @Override
	protected ICrudService<TwaDocAsnHeader, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "smasn";
	}

	@Override
	protected TwaDocAsnHeader newModel(HttpServletRequest request) {
        TwaDocAsnHeader header=new TwaDocAsnHeader();
        header.setCreateSource(CodesConstants.SYSTEM_WMS);
        header.setCategory(XcmgAmConstants.WORK_SOURCE_SELF_MADE);
        header.setDocPrior(WmsConstants.TASK_PRIOR_NORMAL);
		return header;
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
        query.put("category", XcmgAmConstants.WORK_SOURCE_SELF_MADE);
		super.prepareDatatableConditions(query, input);
	}

    @GetMapping("del/{id}")
    public String del(@PathVariable("id") Long id, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (id != null) {
            msg=this.delete(id, true, false);
            if (!StringUtils.isNullOrBlank(msg)){
                addErrorMessage(model, "删除失败:"+msg);
                return super.redirect("");
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return super.redirect("");
    }

    @PostMapping("del")
    public String del(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (ids != null && ids.length > 0) {
            for (Long id:ids){
                msg=this.delete(id, true, false);
                if (!StringUtils.isNullOrBlank(msg)){
                    addErrorMessage(model, "删除失败:"+msg);
                    return super.redirect("");
                }
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return super.redirect("");
    }

    @GetMapping("close/{id}")
    public String close(@PathVariable("id") Long id, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (id != null) {
            msg=this.close(id, true, false);
            if (!StringUtils.isNullOrBlank(msg)){
                addWarnMessage(model, "关闭失败:"+msg);
                return super.redirect("");
            }
            addSuccessMessage(redirectAttributes, "关闭数据成功");
        }
        return super.redirect("");
    }

    @PostMapping("close")
    public String close(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (ids != null && ids.length > 0) {
            for (Long id:ids){
                msg=this.close(id, true, false);
                if (!StringUtils.isNullOrBlank(msg)){
                    addWarnMessage(model, "关闭失败:"+msg);
                    return super.redirect("");
                }
            }
            addSuccessMessage(redirectAttributes, "关闭数据成功");
        }
        return super.redirect("");
    }

    @Override
    protected String preSaveCheck(TwaDocAsnHeader header){
        return super.preSaveCheck(header);
    }

    @Override
    protected String afterSavePopulate(TwaDocAsnHeader header, String worker){
        return super.afterSavePopulate(header, worker);
    }

    protected String redirect(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return "redirect:" + this.referUrl() + relativeUrl;
    }

    protected String root(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return this.referUrl() + relativeUrl;
    }

    private String referUrl(){
        return "/inbound/smasn/";
    }

}
